package com.china08.yxyapi.service.yxyresource.statistics.impl;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceAllCount1Service;

@Service
public class ResourceAllCount1ServiceImpl implements ResourceAllCount1Service {

    @Autowired
    private ElasticsearchTemplate et;

    @Override
    public List<Long> statisticsResourceCount(String districtCode, Integer schoolBureauLevel) {

        // 资源总数量
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        TermQueryBuilder builderTypestauts = new TermQueryBuilder("isDelete", 0);
        boolQueryBuilder.must(builderTypestauts);
        TermQueryBuilder builderDel = null;
        if (schoolBureauLevel == 0) {
            builderDel = new TermQueryBuilder("districtCode", districtCode);
        } else if (schoolBureauLevel == 1) {
            builderDel = new TermQueryBuilder("cityCode", districtCode);
        } else if (schoolBureauLevel == 2) {
            builderDel = new TermQueryBuilder("provinceCode", districtCode);
        }
        boolQueryBuilder.must(builderDel);

        SearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();
        long allCount = et.count(query, ElasticYxyResResource.class);
        List<Long> longCount = new ArrayList<Long>();
        longCount.add(allCount);

        // 共享资源数
        BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
        TermQueryBuilder builderTypestauts1 = new TermQueryBuilder("isDelete", 0);
        boolQueryBuilder1.must(builderTypestauts1);
        TermQueryBuilder builderViewScope = new TermQueryBuilder("viewScope", 0);
        boolQueryBuilder1.mustNot(builderViewScope);

        if (schoolBureauLevel == 0) {
            builderTypestauts = new TermQueryBuilder("districtCode", districtCode);
        } else if (schoolBureauLevel == 1) {
            builderTypestauts = new TermQueryBuilder("cityCode", districtCode);
        } else if (schoolBureauLevel == 2) {
            builderTypestauts = new TermQueryBuilder("provinceCode", districtCode);
        }
        boolQueryBuilder1.must(builderTypestauts);

        SearchQuery query1 = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder1).build();

        long shareCount = et.count(query1, ElasticYxyResResource.class);
        longCount.add(shareCount);

        // 区域资源数
        BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder();
        TermQueryBuilder builderTypestauts2 = new TermQueryBuilder("isDelete", 0);
        boolQueryBuilder2.must(builderTypestauts2);

        TermQueryBuilder builderViewScope1 = new TermQueryBuilder("viewScope", 2);
        boolQueryBuilder2.must(builderViewScope1);

        if (schoolBureauLevel == 0) {
            builderTypestauts = new TermQueryBuilder("districtCode", districtCode);
        } else if (schoolBureauLevel == 1) {
            builderTypestauts = new TermQueryBuilder("cityCode", districtCode);
        } else if (schoolBureauLevel == 2) {
            builderTypestauts = new TermQueryBuilder("provinceCode", districtCode);

        }
        boolQueryBuilder2.must(builderTypestauts);
        SearchQuery query2 = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder2).build();

        long areaCount = et.count(query2, ElasticYxyResResource.class);
        longCount.add(areaCount);
        return longCount;
    }

}
